home *** CD-ROM | disk | FTP | other *** search
/ CD Classic 39 / CD CLASSIC #39 (1998).iso / EMPRESA / visio / Vistdstd / Install / Data.Z / Generic.CPP < prev    next >
C/C++ Source or Header  |  1996-09-27  |  9KB  |  345 lines

  1. /*    GENERIC.CPP - Source for Automation Demo Code in C++
  2.  *  Copyright (C) 1991-1996 Visio Corporation. All rights reserved.
  3.  *
  4.  *
  5.  *    You have a royalty-free right to use, modify, reproduce and distribute
  6.  *    the Sample Application Files (and/or any modified version) in any way
  7.  *    you find useful, provided that you agree that Visio has no warranty,
  8.  *    obligations or liability for any Sample Application Files.
  9.  *
  10.  */
  11.  
  12. #include "visiwrap.h"    //    Visio wrapper classes and utilities
  13.  
  14. #include "resource.h"    //    for our trivial 'About' dialog
  15.  
  16.  
  17. //*****************************************************************************
  18. //
  19. // These declare the event handling procedures we're going to tell our event
  20. // sinks to call when they receive notifications from Visio. The signature
  21. // of ReceiveNotifyFromVisio must conform to VISEVENTPROC. See "addsink.h"
  22.  
  23. static long stc_nEventID= visEvtIDInval;
  24.  
  25. HRESULT STDMETHODCALLTYPE ReceiveNotifyFromVisio (
  26.     IUnknown FAR*    ipSink,
  27.     WORD            wEvent,
  28.     IUnknown FAR*    ipSource,
  29.     DWORD            nEventID,
  30.     DWORD            dwEventSeq,
  31.     IUnknown FAR*    ipSubject,
  32.     VARIANT            eventExtra);
  33.  
  34.  
  35. static long stc_nAnotherEventID= visEvtIDInval;
  36.  
  37. HRESULT STDMETHODCALLTYPE ReceiveAnotherNotifyFromVisio (
  38.     IUnknown FAR*    ipSink,
  39.     WORD            wEvent,
  40.     IUnknown FAR*    ipSource,
  41.     DWORD            nEventID,
  42.     DWORD            dwEventSeq,
  43.     IUnknown FAR*    ipSubject,
  44.     VARIANT            eventExtra);
  45.  
  46.  
  47. //*****************************************************************************
  48. //
  49. //    Handy little "error handling" macro...
  50. //    Requires a "CU:" (clean-up) label somewhere in the function
  51.  
  52. #define check_valid(hr, obj)                    \
  53.     if ( !SUCCEEDED(hr) || !((obj).IsSet()) )    \
  54.         goto CU;
  55.  
  56.  
  57. //*****************************************************************************
  58. //
  59. //    RunDemo
  60. //
  61. //    This function gets called when the user chooses this add-on from the Visio
  62. //    add-ons menu.
  63.  
  64. extern "C" int RunDemo(void)
  65. {
  66.     //    Two non-wrapped event sinks which we must
  67.     //    'Release' manually:
  68.     IUnknown FAR        *pSink= NULL;
  69.     IUnknown FAR        *pAnotherSink= NULL;
  70.     HRESULT                hr= NOERROR;
  71.     static BOOL            bFirstTime= TRUE;    //    only add event callbacks once...
  72.     BOOL                bMadeIt= FALSE;
  73.  
  74.     //    The rest of these will be 'Release'd (if necessary) when
  75.     //    the function exits and they go out of scope:
  76.     CVisioApplication    app;
  77.     CVisioEventList        eList;
  78.     CVisioEvent            event;
  79.     CVisioEvent            anotherEvent;
  80.     CVisioDocuments        docs;
  81.     CVisioDocument        doc;
  82.     CVisioPages            pages;
  83.     CVisioPage            page;
  84.     CVisioShape            shape;
  85.     CVisioShape            shape1;
  86.     CVisioMasters        masters;
  87.     CVisioMaster        master;
  88.     CVisioDocument        stencil;
  89.     CVisioCell            cell;
  90.     CVisioCell            cell1;
  91.  
  92.  
  93.     if ( VAO_SUCCESS != vaoGetObjectWrap(app) )
  94.         goto CU;
  95.  
  96.  
  97.     //    Add a 'Document Created' event notification and a 'Shape Added' notification
  98.     //    to the Application's EventList:
  99.  
  100.     if (bFirstTime && (SUCCEEDED(app.EventList(eList))))
  101.     {
  102.         bFirstTime= FALSE;
  103.  
  104.         if (SUCCEEDED(CoCreateAddonSink(ReceiveNotifyFromVisio, &pSink)))
  105.         {
  106.             if (SUCCEEDED(eList.AddAdvise(visEvtCodeDocCreate, VVariant(pSink),
  107.                                                     VBstr(""), VBstr(""), event)))
  108.             {
  109.                 event.ID(&stc_nEventID);
  110.             }
  111.  
  112.             pSink->Release();
  113.             pSink= NULL;
  114.         }
  115.  
  116.         if (SUCCEEDED(CoCreateAddonSink(ReceiveAnotherNotifyFromVisio, &pAnotherSink)))
  117.         {
  118.             if (SUCCEEDED(eList.AddAdvise((short)(visEvtShape|visEvtAdd), VVariant(pAnotherSink),
  119.                                                     VBstr(""), VBstr(""), anotherEvent)))
  120.             {
  121.                 anotherEvent.ID(&stc_nAnotherEventID);
  122.             }
  123.  
  124.             pAnotherSink->Release();
  125.             pAnotherSink= NULL;
  126.         }
  127.     }
  128.  
  129.  
  130.     //    Add a new Document based on "sample.vst" and then drop two shapes
  131.     //    and glue them together...
  132.  
  133.     hr= app.Documents(docs);
  134.     check_valid(hr, docs);    
  135.  
  136.     hr= docs.Add(VBstr("sample.vst"), doc);
  137.     check_valid(hr, doc);
  138.  
  139.     hr= doc.Pages(pages);
  140.     check_valid(hr, pages);
  141.  
  142.     hr= pages.Item(VVariant(1L), page);
  143.     check_valid(hr, page);
  144.  
  145.     hr= docs.Item(VVariant("sample.vss"), stencil);
  146.     check_valid(hr, stencil);
  147.  
  148.     hr= stencil.Masters(masters);
  149.     check_valid(hr, masters);
  150.  
  151.     hr= masters.Item(VVariant("Executive"), master);
  152.     check_valid(hr, master);
  153.  
  154.     hr= page.Drop(master, 6.0, 6.0, shape);
  155.     check_valid(hr, shape);
  156.  
  157.     //    Re-using "master" without explicit 'Release' is OK --
  158.     //    wrapper classes take care of it for you
  159.     hr= masters.Item(VVariant("Position"), master);
  160.     check_valid(hr, master);
  161.  
  162.     hr= page.Drop(master, 3.0, 3.0, shape1);
  163.     check_valid(hr, shape1);
  164.  
  165.     hr= shape.Cells(VBstr("Connections.X4"), cell);
  166.     check_valid(hr, cell);
  167.  
  168.     hr= shape1.Cells(VBstr("Controls.X1"), cell1);
  169.     check_valid(hr, cell1);
  170.  
  171.        hr= cell1.GlueTo(cell);
  172.  
  173.     bMadeIt= TRUE;
  174.  
  175.  
  176. CU:
  177.     if (!bMadeIt)
  178.         MessageBox(NULL, "Premature termination of generic. Is sample.vst/vss in the path?", "Generic", MB_OK);
  179.  
  180.     return 0;
  181. }
  182.  
  183.  
  184. //*****************************************************************************
  185. //
  186. //    ReceiveNotifyFromVisio
  187. //
  188. //    This function gets called when Visio creates a document.
  189.  
  190. HRESULT STDMETHODCALLTYPE ReceiveNotifyFromVisio (
  191.     IUnknown FAR*    ipSink,        // i: calling sink [assert]
  192.     WORD            wEvent,     // i: code of firing event (visEvt*; visconst.h)
  193.     IUnknown FAR*    ipSource,     // i: object firing event [don't assert]
  194.     DWORD            nEventID,    // i: id of event in ipsource that's firing
  195.     DWORD            dwEventSeq,    // i: sequence number of event that's firing
  196.     IUnknown FAR*    ipSubject,    // i: object event is about [don't assert]
  197.     VARIANT            eventExtra)    // i: other info about event
  198. {
  199.     BOOL bUnexpected= FALSE;
  200.  
  201.     MessageBeep(MB_OK);
  202.  
  203.  
  204.     //    QueryInterface for the Application interface on the IUnknown ipSource.
  205.     //    The ipSource interface pointer, if it is non-NULL, points to
  206.     //    the Application which owns the EventList which fired this event.
  207.     //    To get the Event object, use the EventList::ItemFromID method.
  208.  
  209.     if (ipSource!=NULL)
  210.     {
  211.         LPVISIOAPPLICATION pApp= NULL;
  212.  
  213.         bUnexpected= TRUE;    //    assume until...
  214.  
  215.         if (SUCCEEDED(ipSource->QueryInterface(IID_IVApplication, (LPVOID *) &pApp)))
  216.         {
  217.             CVisioApplication app(pApp);
  218.             CVisioEventList eList;
  219.             CVisioEvent evt;
  220.  
  221.             if (SUCCEEDED(app.EventList(eList)) && eList.IsSet())
  222.             {
  223.                 if (SUCCEEDED(eList.ItemFromID(stc_nEventID, evt)) && evt.IsSet())
  224.                 {
  225.                     short wEventProp;
  226.                     if (SUCCEEDED(evt.getEvent(&wEventProp)))
  227.                     {
  228.                         if (wEventProp==wEvent)
  229.                             bUnexpected= FALSE;        //    ...proven otherwise
  230.                     }
  231.                 }
  232.             }
  233.  
  234.             pApp->Release();
  235.         }
  236.     }
  237.  
  238.  
  239.     //    QueryInterface for the document interface on the IUnknown ipSubject.
  240.     //    The ipSubject interface pointer, if it is non-NULL, should be able to give us
  241.     //    back a Document since we signed up for a 'Document Created' event.
  242.  
  243.     if (ipSubject!=NULL)
  244.     {
  245.         LPVISIODOCUMENT pDoc= NULL;
  246.  
  247.         bUnexpected= TRUE;    //    assume until...
  248.  
  249.         if (SUCCEEDED(ipSubject->QueryInterface(IID_IVDocument, (LPVOID *) &pDoc)))
  250.         {
  251.             CVisioDocument doc(pDoc);
  252.             short objType;
  253.  
  254.             if ( doc.IsSet() && SUCCEEDED(doc.ObjectType(&objType)) && (objType==visObjTypeDoc) )
  255.             {
  256.                 short sSaved;
  257.                 if ( SUCCEEDED(doc.getSaved(&sSaved)) && sSaved )
  258.                     bUnexpected= FALSE;        //    ...proven otherwise
  259.             }
  260.  
  261.             pDoc->Release();
  262.         }
  263.     }
  264.  
  265.  
  266.     //    Screech if unexpected condition was encountered.
  267.     if ( bUnexpected )
  268.     {
  269.         for ( int i = 0; i < 30; i++ )
  270.             MessageBeep((UINT)-1);
  271.     }
  272.  
  273.     return NOERROR;
  274. }
  275.  
  276.  
  277. //*****************************************************************************
  278. //
  279. //    ReceiveAnotherNotifyFromVisio
  280. //
  281. //    This function gets called whenever Visio drops a new shape.
  282.  
  283. HRESULT STDMETHODCALLTYPE ReceiveAnotherNotifyFromVisio ( 
  284.     IUnknown FAR*    ipSink,        // i: calling sink [assert]
  285.     WORD            wEvent,     // i: code of firing event (visEvt*; visconst.h)
  286.     IUnknown FAR*    ipSource,     // i: object firing event [don't assert]
  287.     DWORD            nEventID,    // i: id of event in ipsource that's firing
  288.     DWORD            dwEventSeq,    // i: sequence number of event that's firing
  289.     IUnknown FAR*    ipSubject,    // i: object event is about [don't assert]
  290.     VARIANT            eventExtra)    // i: other info about event
  291. {
  292.     return NOERROR;
  293. }
  294.  
  295.  
  296. /******************************************************************************
  297.  *+ AboutDlgProc
  298.  *
  299.  */
  300. #ifdef __BORLANDC__
  301. #pragma argsused
  302. #endif 
  303.  
  304. #ifdef _DLL
  305. BOOL __loadds CALLBACK AboutDlgProc(
  306. #else
  307. BOOL CALLBACK AboutDlgProc(
  308. #endif
  309.     HWND hDlg,
  310.     UINT msg,
  311.     WPARAM wParam,
  312.     LPARAM lParam)
  313. {
  314.     switch (msg)
  315.        {
  316.         case WM_COMMAND:
  317.             switch (wParam)
  318.                {
  319.                 case IDOK:
  320.                 case IDCANCEL:
  321.                     EndDialog(hDlg, TRUE);
  322.                     return (TRUE);
  323.             }
  324.        }
  325.     
  326.     return (FALSE);
  327. }
  328.  
  329.  
  330. /******************************************************************************
  331.  *+ ShowAboutDialog
  332.  *
  333.  *    Displays the help about dialog.
  334.  */
  335. extern "C" void ShowAboutDialog(HINSTANCE hInstance)
  336. {
  337.     FARPROC lpProcDlg= NULL;
  338.     
  339.     lpProcDlg= MakeProcInstance((FARPROC)AboutDlgProc, hInstance);
  340.  
  341.     DialogBox(hInstance, "ABOUTBOX", NULL, lpProcDlg);
  342.            
  343.     FreeProcInstance(lpProcDlg);
  344. }
  345.